package com.betty.customprovider;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.Nullable;

/**
 * Created by Betty on 2016/12/11.
 */

public class CustomProvider extends ContentProvider{
    SQLiteDatabase db;
    private static final  int PERSON = 1;
    private static final int PERSON_NUMBER = 2;
    private static final int PERSON_TEXT = 3;
    //uri:   content://host/path
    private static UriMatcher macher = new UriMatcher(UriMatcher.NO_MATCH);
    static {
        macher.addURI("betty","person",PERSON);// content://betty/person
        macher.addURI("betty","person/#",PERSON_NUMBER);// content://betty/person/2   #表示任意数字
        macher.addURI("betty","person/filter/*",PERSON_TEXT);// content://betty/person/filter/ddd;  *表示任意文本值
    }
    @Override
    public boolean onCreate() {
        DBHelper dbHelper = new DBHelper(getContext());
        db = dbHelper.getWritableDatabase();//建库建表
        return true;
    }

    @Nullable
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        int uriID = macher.match(uri);
        switch (uriID){
            case PERSON:
               Long insert = db.insert("person",null,values);
               return ContentUris.withAppendedId(uri,insert);//将原有的uri和id进行拼接，形成一个新的uri;
            case PERSON_NUMBER:
                break;
            case PERSON_TEXT:
                break;
        }
        return null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        return 0;
    }

    @Nullable
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        int matcher=macher.match(uri);
        switch (matcher){
            case PERSON:
                return db.query("person",projection,selection,selectionArgs,null,null,sortOrder);

            case PERSON_NUMBER:
                break;
            case PERSON_TEXT:
                break;
        }
        return null;
    }

    @Nullable
    @Override
    public String getType(Uri uri) {
        return null;
    }
}
